/*
* File: logging.h
* Author: Henrik Wistbacka
* --------------------------
* This header file is describing the interface
* to be able to log network connections from a client
* to a server.
*
* The parameters that are logged in logfile is:
* Domain    Port     Date  Time   ClientCommand   ServerResponse   SentBytes
* Please see respective description of functions for 
* logging to file. The prototypes in this
* interface are:
*
* errorT initiliseLogging(char * logfileName);
* errorT getFullLogFile(&length, &logMessage);
* errorT setDomainAndPortName(struct sockaddr clientAddr, int clientAddrLen, logT log);
* errorT saveLogToFile(logT log);
* errorT endLogging();
*/

#ifndef LOGGING_H
#define LOGGING_H

#define _CRT_SECURE_NO_WARNINGS
/*
* Type definition: logT
* --------------------
* Struct logT contains the parameters used in the 
* communication between the client and the server.
* All the fields in the struct are belonging to 
* the client, except when explicitly set prefix to 
* server****.
* domainName is the domain name e.g "www.bt.se". 
* portName is the port e.g 29567. 
* contactDate is the date when the log was saved to
* logfile.  
* contactTime is the time when the log was saved to 
* file.
* HTTPCommand is the command the client sent to 
* the server.
* serverHTTPAnswer is the server's answer to the 
* command from the client.
* serverSentBytes is the total sent bytes of a file 
* from the server to the client.
* reserved is a reserved field for future use. Do
* not use it.
*/
typedef struct {
	char *domainName;
	char *portName;
	char *contactDate;
	char *contactTime;
	char *HTTPCommand;
	char *serverHTTPAnswer;
	int  serverSentBytes;
	void *reserved;
} *logT;


/*
* Type definition: errorT
* --------------------
* Enum errorT contains the error codes
* that a function can return 
* when a failure happened.
*/
typedef enum{
	allOK			 =  0,
	unknownError	 = -3,
	streamError		 = -2,
	initNotPerformed = -1,
	filenameTooShort = 20,
	fileOpenError	 = 21,
	failedMutex		 = 22,
	seekError		 = 23,
	logFileEmpty	 = 24,
	fileError		 = 25, 
	stringBuildError = 26,
	domainError		 = 27,
	fileNotFound	 = 28,
	commanNotFound   = 29, 
	unknownContent	 = 30, 
	fileNotModified  = 31
} errorT;

/*
* Function: initiliseLogging
* Usage: error = initiliseLogging(logfileName, mode);
* ------------------------------------------
* This function MUST be the first function used,
* before any other function or function from this
* interface can be used. logfileName is set to the
* name that is wanted for the logfile. mode can be
* set to either "ab+" or "wb+".
* If mode is set to "ab+" and file exists, 
* all logs written to file is appended to the end
* of the file. If mode is set to "ab+" and file
* does not exist - a new file is created and all
* logs written to it will be appended to the file.
* If mode is set to "wb+" and file exists, the contents 
* of the file is destroyed and all logs are written to the
* file. If mode is set to "wb+" and file does not exists, 
* a new file is opened and logs are written to it.
*/
errorT initiliseLogging(const char *logfileName, const char *mode);

/*
* Function: getFullLogFile
* Usage: error = getFullLogFile(length, logMessage);
* ------------------------------------------
* This function returns the full content of the 
* logfile, that was named in initiliseLogging, as
* a pointer to the first character in logMessage. The 
* length of the returned string is returned in length, 
* which is set to the number of characters in the string, 
* exclusive the trailing '\0'.
*/
errorT getFullLogFile(int *length, char  **logMessage);

/*
* Function: setDomainAndPortName
* Usage: error = setDomainAndPortName(clientAddr, clientAddrLen, log);
* ------------------------------------------
* This function sets the domainName and portName in the log.
* The values is extracted from clientAddr, which has to be a 
* valid sockaddr used in the function accept(....) that is 
* located in header Winsock2.h. clientAddrLen is the length that
* is returned from accept(....).
*/
errorT setDomainAndPortName(struct sockaddr clientAddr, int clientAddrLen, logT log);

/*
* Function: saveLogToFile
* Usage: error = saveLogToFile(log);
* ------------------------------------------
* This function saves all the fields in log to file, that
* has been set in initiliseLogging(....).
*/
errorT saveLogToFile(logT log);

/*
* Function: endLogging
* Usage: error = endLogging();
* ------------------------------------------
* This function closes the logfile and releases
* all resources allocated when initiliseLogging(....)
* was called. To be able to log to file again, call
* initiliseLogging(....) again.
*/
errorT endLogging();


/*
* Function: getHTMLTime
* Usage: error = getHTMLTime(HTMLTime);
* ------------------------------------------
* This function returns the current time in format
* that is applicable to HTML Date response.
*/
errorT getHTMLTime(char **HTMLTime);

#endif